package com.test.springboot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice //全局捕获异常的控制器
public class BaseExcepetionHander {
	
	@Autowired
	HttpServletRequest request;

	@ExceptionHandler(Exception.class)
	public String allExcepetions(Exception e){
		System.out.println(e);
		
		if(e instanceof ArithmeticException){
			request.setAttribute("msg", "除数不能为零");
		} else if(e instanceof ArrayIndexOutOfBoundsException){
			request.setAttribute("msg", "索引越界");
		}else {
			request.setAttribute("msg", "服务器内部错误");
		}
		
		
		return "errors/500";
	}
}
